﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using NHibernate;
using NHibernate.Criterion;
using BotonJXC.Util;

namespace BotonJXC.DAO
{
    public class ReturnOrdersDao : NHibernateDao<ReturnedBlls, int>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public IList<ReturnedBlls> findReturnedBllsBySn(string ReturnedSn)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(ReturnedSn))
            {
                criterions.Add(Restrictions.Eq("ReturnedSn", ReturnedSn));
            }
            return base.Find(criterions.ToArray());
        }

        public void saveReturnOrders(ReturnedBlls ReturnedBlls)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Save(ReturnedBlls);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;

            }
            finally
            {
                HBHelper.Close();
            }

        }

        public void updateReturnOrders(ReturnedBlls ReturnedBlls)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                base.Update(ReturnedBlls);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                HBHelper.Close();
            }

        }
        public void delReturnOrders(ReturnedBlls ReturnedBlls)
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                //  base.Delete(pip.PriceInfo.PriceInfoId);
                base.Delete(ReturnedBlls);
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                HBHelper.Close();
            }
        }

        public PageModel<ReturnedBlls> FindPage(string ReturnedSn, int SupplierId, int WarehouseId, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(ReturnedSn))
            {
                criterions.Add(Restrictions.Eq("ReturnedSn", ReturnedSn));
            }
            if ( SupplierId > 0 )
            {
                criterions.Add(Restrictions.Eq("Supplier.SupplierId", SupplierId));
            }
            Order order = new Order("CreateTime", false);
            return base.FindByPage(criterions.ToArray(), order, start, count);
        }
    }
}
